package com.tuyan.aixiang.activity.fullplayer;

import com.tuyan.aixiang.common.BasePresenter;
import com.tuyan.aixiang.model.YsDevice;
import com.tuyan.aixiang.util.CollectionUtils;
import com.tuyan.aixiang.util.ToastUtil;
import com.videogo.exception.BaseException;
import com.videogo.openapi.EZDeviceApiManager;
import com.videogo.openapi.EZOpenSDK;
import com.videogo.openapi.bean.EZDeviceInfo;

import rx.Observable;
import rx.Subscriber;

public class FullPlayerPresenter extends BasePresenter<FullPlayerActivity> implements FullPlayer.Presenter {

    private YsDevice ysDevice;
    private EZDeviceInfo deviceInfo;

    @Override
    public void init(YsDevice device) {
        this.ysDevice = device;
//        iView.get().initPlayer();
        iView.get().initPlayer();
        getDeviceInfo();

    }

    @Override
    public boolean isValidDevice() {
        if (null != deviceInfo) {
            if (!CollectionUtils.isEmpty(deviceInfo.getCameraInfoList())) {
                return true;
            }
        }
        return false;
    }

    @Override
    public Integer getSupportTalkMode() {
        if(!isValidDevice()) return 0;
        return deviceInfo.supportTalkMode();
    }

    public void getDeviceInfo() {
        iView.get().showLoading();
        Observable observable = Observable.create((Observable.OnSubscribe<EZDeviceInfo>) subscriber -> {
            try {
                EZDeviceInfo info = EZDeviceApiManager.getDeviceInfo(ysDevice.getDeviceSerial());
                deviceInfo = info;
                subscriber.onNext(deviceInfo);
            } catch (BaseException e) {
                subscriber.onError(e);
            }
        });

        subscribeAsync(observable, new Subscriber<EZDeviceInfo>() {
            @Override
            public void onCompleted() {
//                mPlayView.dismissLoadDialog();
                iView.get().hideLoading();
            }

            @Override
            public void onError(Throwable e) {
                iView.get().hideLoading();
                e.printStackTrace();
                ToastUtil.makeShort("获取设备信息失败");
//                onErrorBaseHandle(((BaseException) e).getErrorCode());
            }

            @Override
            public void onNext(EZDeviceInfo ret) {
                iView.get().hideLoading();
                if (null != ret && isValidDevice()) {
                    int level = ret.getCameraInfoList().get(0).getVideoLevel();
                    if (level > 0) {
                        iView.get().setQuilityResult(true);
                    } else {
                        iView.get().setQuilityResult(false);
                    }

                    iView.get().setTalkModeEnable(ret.supportTalkMode() > 0);
                    iView.get().setPTZEnable(ret.isSupportPTZ());
                } else {

                }
            }
        });

    }

    @Override
    public void setQuality(boolean hd) {
//        mPlayView.showLoadDialog();
        iView.get().showLoading();

        int videoLevel = hd ? 2 : 0;     // 0-流畅，1-均衡，2-高清，3-超清

        Observable observable = Observable.create((Observable.OnSubscribe<Boolean>) subscriber -> {
            try {
                boolean ret = EZOpenSDK.setVideoLevel(ysDevice.getDeviceSerial(), ysDevice.getChannelNo(), videoLevel);
                subscriber.onNext(ret);
            } catch (BaseException e) {
                e.printStackTrace();
                subscriber.onError(e);
            }
        });
        subscribeAsync(observable, new Subscriber<Boolean>() {
            @Override
            public void onCompleted() {
//                mPlayView.dismissLoadDialog();
                iView.get().hideLoading();
            }

            @Override
            public void onError(Throwable e) {
                iView.get().hideLoading();
                e.printStackTrace();
                ToastUtil.makeShort("清晰度切换失败");
                iView.get().setQuilityResult(!hd);
//                onErrorBaseHandle(((BaseException) e).getErrorCode());
            }

            @Override
            public void onNext(Boolean ret) {
                iView.get().hideLoading();
                if (ret) {
                    iView.get().setQuilityResult(hd);
                } else {
                    iView.get().setQuilityResult(!hd);
                    ToastUtil.makeShort("清晰度切换失败");
                }
            }
        });
    }

}
